module executor.group.CreateGroupExecutor;

import msgtrans;
import message.Constants;
import cache.UserStatusManager;
import hunt.logging;
import google.protobuf;
import message.MsgDefine;
import client.LogicServerClient;
import client.GateServerClient;
import std.array;
import std.conv : to;

@TransportClient(LogicClientName)
class CreateGroupExecutor : AbstractExecutor!(CreateGroupExecutor) {

  @MessageId(MESSAGE.CMD_JOIN_GROUP)
  void joinGroup(TransportContext ctx, MessageBuffer buffer)
  {
    JoinGroup joinGroup = new JoinGroup;
    buffer.data.fromProtobuf!JoinGroup(joinGroup);
    logInfof("joinGroup");
    if (UserStatusManager.getInstance().getUserStatus(joinGroup.destination))
    {
      auto contexts = GateServerClient.getInstance.getAllClient();
      foreach(TransportSession s ; contexts)
      {
        s.send(new MessageBuffer(MESSAGE.CMD_JOIN_GROUP,joinGroup.toProtobuf.array));
      }
    }
  }

  @MessageId(MESSAGE.CMD_APPLY_GROUP)
  void applyGroup(TransportContext ctx, MessageBuffer buffer)
  {
    ApplyGroup applyGroup = new ApplyGroup;
    buffer.data.fromProtobuf!ApplyGroup(applyGroup);
    logInfof("applyGroup");
    if (UserStatusManager.getInstance().getUserStatus(applyGroup.destination))
    {
      auto contexts = GateServerClient.getInstance.getAllClient();
      foreach(TransportSession s ; contexts)
      {
        s.send(new MessageBuffer(MESSAGE.CMD_APPLY_GROUP,applyGroup.toProtobuf.array));
      }
    }else
    {
      MessageTransportClient logicClient = LogicServerClient.getInstance().getForwardClient();
      logicClient.send(new MessageBuffer(MESSAGE.CMD_OFFLINE_INVITE_GROUP,applyGroup.toProtobuf.array));
    }
  }


  @MessageId(MESSAGE.CMD_INVITE_GROUP)
  void inviteGroup(TransportContext ctx, MessageBuffer buffer)
  {
    //InviteGroup inviteGroup = new InviteGroup;
    //buffer.data.fromProtobuf!InviteGroup(inviteGroup);
    //logInfof("inviteGroup");
    //if (UserStatusManager.getInstance().getUserStatus(inviteGroup.destination))
    //{
    //  auto contexts = GateServerClient.getInstance.getAllClient();
    //  foreach(TransportSession s ; contexts)
    //  {
    //    s.send(new MessageBuffer(MESSAGE.CMD_INVITE_GROUP,inviteGroup.toProtobuf.array));
    //  }
    //}else
    //{
    //  MessageTransportClient logicClient = LogicServerClient.getInstance().getForwardClient();
    //  logicClient.send(new MessageBuffer(MESSAGE.CMD_OFFLINE_INVITE_GROUP,inviteGroup.toProtobuf.array));
    //}
  }
}




